﻿package 
{
	import fl.events.SliderEvent;
	import fl.events.ColorPickerEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	
	import idv.etrex.display.*;
	import idv.etrex.display.object.*;
	
	/**
	 * ...
	 * @author etrex
	 */
	public class MultiGraphics2 extends Sprite 
	{
		var canvas:Xhape = new Xhape();
		var selectedColor:uint = 0;
		var selectedStrokeSize:int = 3;
		
		public function MultiGraphics2():void 
		{			
			// new a canvas
			canvas.x = drawArea.x;
			canvas.y = drawArea.y;
			canvas.scaleX = drawArea.scaleX;
			canvas.scaleY = drawArea.scaleY;
			this.addChild(canvas);
			
			// add multi graphics
			for (var i:int = 0 ; i < container.numChildren ; i++)
				canvas.graphicsBat.add(new FlashGraphics(MovieClip(container.getChildAt(i)).c.graphics));
			
			//set initial line style
			canvas.graphicsBat.lineStyle(selectedStrokeSize,selectedColor);
			
			// add mouse paint event
			drawArea.addEventListener(MouseEvent.MOUSE_DOWN, whenMouseDown);
			drawArea.addEventListener(MouseEvent.MOUSE_UP, whenMouseUp);
			stage.addEventListener(MouseEvent.MOUSE_UP, whenMouseUp);
			
			// add clearButton click event
			clearButton.addEventListener(MouseEvent.CLICK, clearButtonClick);
			
			// add slider change event
			slider.addEventListener(SliderEvent.CHANGE, sliderChanged);
			
			// add colorPicker change event
			colorPicker.addEventListener(Event.CHANGE,colorChange);
		}
		
		// mouse paint event
		public function whenMouseDown(e:MouseEvent):void {
			
			canvas.graphicsBat.moveTo(e.localX, e.localY);
			drawArea.addEventListener(MouseEvent.MOUSE_MOVE, whenMouseMove);
		}
		public function whenMouseUp(e:MouseEvent):void {
			drawArea.removeEventListener(MouseEvent.MOUSE_MOVE, whenMouseMove);
		}
		public function whenMouseMove(e:MouseEvent):void {
			trace(e.localX, e.localY);
			canvas.graphicsBat.lineTo(e.localX, e.localY);
		}
		
		public function clearButtonClick(e:MouseEvent):void {
			//clear the canvas
			canvas.graphicsBat.clear();
			canvas.graphicsBat.lineStyle(selectedStrokeSize,selectedColor);
		}
		
		//select stroke size
		public function sliderChanged(e:SliderEvent):void {
            selectedStrokeSize = e.target.value+1;
            sliderLabel.text = "strokeSize: " + selectedStrokeSize;
			canvas.graphicsBat.lineStyle(selectedStrokeSize,selectedColor);
        }
		
		public function colorChange(e:Event):void {
			selectedColor = ColorPicker(e.target).selectedColor;
			canvas.graphicsBat.lineStyle(selectedStrokeSize,selectedColor);
		}
	}
}